package com.leather.quan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leather.common.entity.Quan;
import com.leather.common.entity.QuanImg;
import com.leather.common.result.CodeMsg;
import com.leather.common.result.Result;
import com.leather.common.toolkits.CheckKit;
import com.leather.common.toolkits.ImageKit;
import com.leather.quan.config.MQConfig;
import com.leather.quan.mapper.QuanImgMapper;
import com.leather.quan.mapper.QuanMapper;
import com.leather.quan.service.QuanService;
import com.leather.quan.vo.QuanQueryVo;
import com.leather.quan.vo.QuanVo;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class QuanServiceImpl extends ServiceImpl<QuanMapper, Quan> implements QuanService {


    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private QuanMapper quanMapper;

    @Autowired
    private QuanImgMapper quanImgMapper;

    @Override
    public Result addQuan(QuanVo quanVo) {
        Quan quan = quanVo.transfer();
        save(quan);
        return Result.success(quan.getId());
    }

    @Override
    @Transactional
    public Result addQuanWithMutiImage(QuanVo quanVo, List<MultipartFile> files) {
        Quan quan = quanVo.transfer();
        save(quan);
        String quanImg;
        for (MultipartFile file : files) {
            quanImg = ImageKit.uploadImage(file, "quan");
            QuanImg quanI = new QuanImg();
            quanI.setQuanId(quan.getId());
            quanI.setImgUrl(quanImg);
            quanImgMapper.insert(quanI);
        }
        return Result.success("success");
    }

    @Override
    @Transactional
    public Result deleteQuan(int quanId,int userId) {
        QueryWrapper<QuanImg> wrapper = new QueryWrapper<>();
        wrapper.eq("quan_id", quanId);
        List<QuanImg> quanImgs = quanImgMapper.selectList(wrapper);
        quanImgMapper.delete(wrapper);
        QueryWrapper<Quan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",quanId).eq("user_id",userId);
        boolean result = remove(queryWrapper);
        CheckKit.check(result, CodeMsg.SERVER_ERROR);
        for (QuanImg quanImg :quanImgs) {
            amqpTemplate.convertAndSend(MQConfig.QUAN_QUENE,quanImg.getImgUrl());
        }
        amqpTemplate.convertAndSend(MQConfig.COMMENT_QUEUE,quanId);
        return Result.success("OK");
    }

    @Override
    @Transactional
    public Result touchStar(int quanId) {
        CheckKit.check(quanId > 0, CodeMsg.ERROR_QUAN_ID);
        int count = quanMapper.addStars(quanId);
        CheckKit.check(count > 0, CodeMsg.SERVER_ERROR);
        return Result.success("ok");
    }

    @Override
    public Result getIndexData(int pageId) {
        return Result.success(quanMapper.indexQuery(pageId));
    }

    @Override
    public Result getQuanByUserId(int pageId, int userId) {
        CheckKit.check(userId > 0, CodeMsg.ERROR_USER_ID);
        return Result.success(quanMapper.getQuanByUserId(pageId, userId));
    }

    @Override
    public Result getQuanDetail(int quanId) {
        CheckKit.check(quanId>0,CodeMsg.ERROR_QUAN_ID);
        QuanQueryVo quan = quanMapper.getQuanById(quanId);
        CheckKit.check(quan!=null,CodeMsg.ERROR_QUAN_ID);
        return Result.success(quan);
    }

    @Override
    public Result searchQuan(String searchValue) {
        CheckKit.notEmpty(searchValue,CodeMsg.EMPTY_SEARCHVALUE_VALUE);
        return Result.success(quanMapper.searchValue(searchValue));
    }

}
